import 'package:flutter/material.dart';
import 'package:x3_base_core/x3_base_core.dart';
import 'package:x3_component_core/utils/base_component_utils.dart';

///**
///* @fileName: base_customize_dropdown_select_dialog
///* @author: hrongyong
///* @date-time: 12/10/21 9:44 AM
///* @email: hrongyong@gmail.com
///* description:
///* <fileName> <author> <date-time> <email> <description>
///* 文件名 作者姓名 创建时间 作者邮箱 文件描述
///*/
class BaseCustomizeDropdownSelectDialog extends StatelessWidget {
  // 全部选项
  final List<BaseKeyValueModel> optionsList;

  // 首次的选项，默认第0位
  final BaseKeyValueModel selectModel;

  // 选中回调
  final Function(BaseKeyValueModel selectModel, int selectIndex) selectCallBack;

  // 预留高度
  final double reservedHeight;

  const BaseCustomizeDropdownSelectDialog({
    Key key,
    @required this.reservedHeight,
    @required this.optionsList,
    @required this.selectCallBack,
    this.selectModel,
  }) : super(key: key);

  ///标题栏（appbar）
  static double _appbarHeight = AppBar().preferredSize.height;

  @override
  Widget build(BuildContext context) {
    return Material(
      type: MaterialType.transparency,
      child: SafeArea(
        child: Column(
          children: [
            SizedBox(height: _appbarHeight + reservedHeight),
            BaseComponentUtils.buildHorizontalDivider(),
            Container(color: Colors.white, constraints: BoxConstraints(maxHeight: MediaQuery.of(context).size.height * 0.7), child: _buildListView()),
            Expanded(child: InkWell(onTap: () => BaseRouterUtil.getInstance().pop(), child: Container(color: Color(0XAA000000)))),
          ],
        ),
      ),
    );
  }

  Widget _buildListView() {
    return ListView.separated(
      shrinkWrap: true,
      itemCount: this.optionsList.length,
      itemBuilder: (BuildContext context, int index) => _buildItem(context, this.optionsList[index], index),
      separatorBuilder: (BuildContext context, int index) => BaseComponentUtils.buildHorizontalDivider(),
    );
  }

  Widget _buildItem(BuildContext context, BaseKeyValueModel model, int index) {
    int _index = this.optionsList.indexWhere((element) => element.key == this.selectModel.key);

    /// 系统颜色 黑 Color(0xff333333) 白：Color(0xffffffff) 黑白主题选中颜色为Color(0xFFFF004F)
    Color _systemColor = Theme.of(context).primaryColor == Colors.white || Theme.of(context).primaryColor == Color(0xFF333333) ? Color(0xFFFF004F) : Theme.of(context).primaryColor;
    return InkWell(
      onTap: () => _onItemEvent(model, index),
      child: Container(
        height: 48,
        alignment: Alignment.centerLeft,
        padding: EdgeInsets.symmetric(horizontal: 16),
        color: _index == index ? Color(0xFFF8F8F8) : Colors.white,
        child: Text(model.value, style: TextStyle(color: _index == index ? _systemColor : Color(0xFF333333))),
      ),
    );
  }

  void _onItemEvent(BaseKeyValueModel model, int index) {
    this.selectCallBack(model, index);
    BaseRouterUtil.getInstance().pop();
  }
}
